//revisi: 20 jan 2010
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.x3.purchasing.common;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author basit adhi prabowo
 */
public class ClassAntiNull
{
    private static SimpleDateFormat DateMySQL = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat DateHuruf = new SimpleDateFormat("dd MMMM yyyy");
    private static SimpleDateFormat DateBalikMysql = new SimpleDateFormat("dd-MM-yyyy");
    private static DecimalFormat HundredComma = new DecimalFormat("#,###.00");
    private static DecimalFormat Hundred = new DecimalFormat("#,###");
    
    public static int AntiNullInt(Object obj)
    {
        return (obj == null) ? 0 : Integer.valueOf(obj.toString()).intValue();
    }

    public static String AntiNullIntString(Object obj)
    {
        return (obj == null) ? "0" : Hundred.format(obj);
    }

    public static String AntiNullIntStringUnformatted(Object obj)
    {
        return (obj == null) ? "0" : obj.toString();
    }

    public static String AntiNullString(Object obj)
    {
        return (obj == null) ? "" : obj.toString();
    }

    public static double AntiNullDouble(Object obj)
    {
        return (obj == null) ? 0.0 : Double.valueOf(obj.toString()).doubleValue();
    }

    public static String AntiNullDoubleString(Object obj)
    {
        return (obj == null) ? "0.0" : Hundred.format(obj);
    }

    public static String AntiNullDoubleCurrency(Object obj)
    {
        return (obj == null) ? "0.0" : HundredComma.format(obj);
    }

    public static String AntiNullDoubleStringUnformatted(Object obj)
    {
        return (obj == null) ? "0.0" : obj.toString();
    }

    public static Date AntiNullDate(Object obj)
    {
        return (obj == null) ? null : Date.class.cast(obj);
    }

    public static String AntiNullDateString(Object obj)
    {
        return (obj == null) ? "00/00/0000" : obj.toString();
    }

    public static String AntiNullDateMysqlString(Object obj)
    {
        return (obj == null) ? "0000-00-00" : DateMySQL.format(obj);
    }

    public static String AntiNullDateHurufString(Object obj)
    {
        return (obj == null) ? "00 00 0000" : DateHuruf.format(obj);
    }

    public static String AntiNullDateBalikMysqlString(Object obj)
    {
        return (obj == null) ? "0000-00-00" : DateBalikMysql.format(obj);
    }

    public static String AntiNullDateTanggalString(Object obj)
    {
        return AntiNullDateBalikMysqlString(obj).substring(0, 2);
    }

    public static String AntiNullDateBulanString(Object obj)
    {
        return AntiNullDateBalikMysqlString(obj).substring(3, 5);
    }

    public static String AntiNullDateTahunString(Object obj)
    {
        return AntiNullDateBalikMysqlString(obj).substring(6);
    }

    public static float AntiNullFloat(Object obj)
    {
        return (obj == null) ? 0 : Float.valueOf(obj.toString()).floatValue();
    }

    public static String AntiNullFloatString(Object obj)
    {
        return (obj == null) ? "0" : Hundred.format(obj);
    }

    public static String AntiNullFloatCurrency(Object obj)
    {
        return (obj == null) ? "0" : HundredComma.format(obj);
    }

    public static String AntiNullFloatStringUnformatted(Object obj)
    {
        return (obj == null) ? "0" : obj.toString();
    }

    public static byte AntiNullByte(Object obj)
    {
        return (obj == null) ? 0 : Byte.valueOf(obj.toString()).byteValue();
    }

    public static String AntiNullByteString(Object obj)
    {
        return (obj == null) ? "0" : obj.toString();
    }

    public static long AntiNullLong(Object obj)
    {
        return (obj == null) ? 0 : Long.valueOf(obj.toString()).longValue();
    }

    public static String AntiNullLongString(Object obj)
    {
        return (obj == null) ? "0" : Hundred.format(obj);
    }

    public static String AntiNullLongStringUnformatted(Object obj)
    {
        return (obj == null) ? "0" : obj.toString();
    }

    public static boolean cekTelpon(String txtTelp)
    {
        int i;
        int c;
        for (i=0; i < txtTelp.length(); i++)
        {
                c = (int) txtTelp.charAt(i);
                if (!((c >= 48 && c <= 57) || c == 45)) break;
        }
        if (i == txtTelp.length()) return true; else return false;
    }

    public static Date tanggalSekarang()
    {
        Calendar tglSekarang = Calendar.getInstance();
        return tglSekarang.getTime();
    }
}
